#include "ui_login.h"

static lv_obj_t * ui_login_srceen;
static lv_obj_t * obj_tv;
static lv_obj_t * textarea_user_name;
static lv_obj_t * textarea_password;
static lv_obj_t * lab_msg;

static void btn_login_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED) {
        lv_label_set_text(lab_msg, "#ff0000 Login succeeded");
        puts(lv_textarea_get_text(textarea_user_name));
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("btn_login_Toggled");
    }
}

static void btn_register_event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
 
    if(code == LV_EVENT_CLICKED) {
        lv_label_set_text(lab_msg, "#ff0000 Register succeeded");
        puts(lv_textarea_get_text(textarea_password));
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("btn_register Toggled");
    }
}



static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    lv_obj_t * kb = lv_event_get_user_data(e);
    if(code == LV_EVENT_FOCUSED) {
        if(lv_indev_get_type(lv_indev_get_act()) != LV_INDEV_TYPE_KEYPAD) {
            lv_keyboard_set_textarea(kb, ta);
            lv_obj_set_style_max_height(kb, LV_HOR_RES * 2 / 3, 0);
            lv_obj_update_layout(obj_tv);   /*Be sure the sizes are recalculated*/
            lv_obj_set_height(obj_tv, 480- lv_obj_get_height(kb));
            lv_obj_set_pos(obj_tv, (800-496)/2, 0);
            lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
            lv_obj_scroll_to_view_recursive(ta, LV_ANIM_OFF);
        }
    }
    else if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_set_height(obj_tv, 296);
        lv_obj_set_pos(obj_tv, (800-496)/2, (480-296)/2);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_indev_reset(NULL, ta);

    }
    else if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL) {
        lv_obj_set_height(obj_tv, 296);
        lv_obj_set_pos(obj_tv, (800-496)/2, (480-296)/2);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_state(ta, LV_STATE_FOCUSED);
        lv_indev_reset(NULL, ta);   /*To forget the last clicked object to make it focusable again*/
    }
}

void ui_login_screen_init(void)
{

    ui_login_srceen = lv_obj_create(NULL);
    
    lv_obj_t * login_bg_img = lv_img_create(ui_login_srceen);
    lv_img_set_src(login_bg_img, "S:/tmp/桌面/myproject5/img/登录背景.bmp");

    lv_obj_t * kb = lv_keyboard_create(ui_login_srceen);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);

    //用户名密码之类的文本样式
    static lv_style_t style_text_muted;
    lv_style_init(&style_text_muted);
    lv_style_set_text_opa(&style_text_muted, LV_OPA_50);

    //按钮样式
    static lv_style_t style_btn;
    lv_style_init(&style_btn);
    lv_style_set_bg_color(&style_btn, lv_color_hex(0x43CD80));

    //提示信息标签样式
    static lv_style_t style_lab_msg;
    lv_style_init(&style_lab_msg);
    lv_style_set_bg_color(&style_lab_msg, lv_color_hex(0xff0000));


    obj_tv = lv_obj_create(ui_login_srceen);
    // lv_obj_remove_style_all(obj_tv);
    lv_obj_set_width(obj_tv, 494);//800*0.618=496   此乃黄金比例
    lv_obj_set_height(obj_tv, 296);
    lv_obj_set_pos(obj_tv, (800-496)/2, (480-296)/2);

    lv_obj_t * lab_chess = lv_label_create(obj_tv);
    lv_label_set_text(lab_chess, "Chinese Chess");

    lv_obj_t * user_name_label = lv_label_create(obj_tv);
    lv_label_set_text(user_name_label, "User name");
    lv_obj_add_style(user_name_label, &style_text_muted, 0);

    textarea_user_name = lv_textarea_create(obj_tv);
    lv_textarea_set_one_line(textarea_user_name, true);
    lv_textarea_set_placeholder_text(textarea_user_name, "Your name");
    lv_obj_add_event_cb(textarea_user_name, ta_event_cb, LV_EVENT_ALL, kb);

    lv_obj_t * password_label = lv_label_create(obj_tv);
    lv_label_set_text(password_label, "Password");
    lv_obj_add_style(password_label, &style_text_muted, 0);

    textarea_password = lv_textarea_create(obj_tv);
    lv_textarea_set_one_line(textarea_password, true);
    lv_textarea_set_password_mode(textarea_password, true);
    lv_textarea_set_placeholder_text(textarea_password, "Min. 8 chars.");
    lv_obj_add_event_cb(textarea_password, ta_event_cb, LV_EVENT_ALL, kb);

    lv_obj_t * btn_login = lv_btn_create(obj_tv);
    lv_obj_t * lab_login = lv_label_create(btn_login);
    lv_label_set_text(lab_login, "Login");

    lv_obj_set_height(btn_login, LV_SIZE_CONTENT);
    lv_obj_add_event_cb(btn_login, btn_login_event_handler, LV_EVENT_ALL, NULL);

    lv_obj_t * btn_register = lv_btn_create(obj_tv);
    lv_obj_t * lab_register = lv_label_create(btn_register);
    lv_label_set_text(lab_register, "Register");

    lv_obj_add_style(btn_register, &style_btn, 0);
    lv_obj_set_height(btn_register, LV_SIZE_CONTENT);
    lv_obj_add_event_cb(btn_register, btn_register_event_handler, LV_EVENT_ALL, NULL);


    lab_msg = lv_label_create(obj_tv);
    // lv_obj_add_style(lab_msg, &style_lab_msg, 0);
    lv_label_set_recolor(lab_msg, true); 
    lv_label_set_text(lab_msg, " ");
    
    static lv_coord_t grid_main_col_dsc[] = { LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
    static lv_coord_t grid_main_row_dsc[] = { 30, LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_FR(1), 30, LV_GRID_TEMPLATE_LAST};

    lv_obj_set_grid_dsc_array(obj_tv, grid_main_col_dsc, grid_main_row_dsc);
    lv_obj_set_grid_cell(lab_chess, LV_GRID_ALIGN_CENTER, 0, 4, LV_GRID_ALIGN_CENTER, 0, 1);

    lv_obj_set_grid_cell(user_name_label, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(textarea_user_name, LV_GRID_ALIGN_CENTER, 1, 3, LV_GRID_ALIGN_CENTER, 1, 1);
    lv_obj_set_grid_cell(password_label, LV_GRID_ALIGN_CENTER, 0, 1, LV_GRID_ALIGN_CENTER, 2, 1);
    lv_obj_set_grid_cell(textarea_password, LV_GRID_ALIGN_CENTER, 1, 3, LV_GRID_ALIGN_CENTER, 2, 1);

    lv_obj_set_grid_cell(btn_login, LV_GRID_ALIGN_CENTER, 0, 2, LV_GRID_ALIGN_CENTER, 3, 1);
    lv_obj_set_grid_cell(btn_register, LV_GRID_ALIGN_CENTER, 2, 2, LV_GRID_ALIGN_CENTER, 3, 1);

    lv_obj_set_grid_cell(lab_msg, LV_GRID_ALIGN_START, 0, 4, LV_GRID_ALIGN_CENTER, 4, 1);

}

void ui_login_init(void)
{
    lv_disp_t * dispp = lv_disp_get_default();
    lv_theme_t * theme = lv_theme_default_init(dispp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED),
                                               false, LV_FONT_DEFAULT);
    lv_disp_set_theme(dispp, theme);
    ui_login_screen_init();
    lv_disp_load_scr(ui_login_srceen);
}
